package com.xiang.publicdemo.module1.config;

import com.xiang.publicdemo.common.utils.SpringContextUtil;
import com.xiang.publicdemo.common.utils.StringUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ServiceConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        String path = "application-module1-service.yml";
        try {
            String profile = SpringContextUtil.getActiveProfile();
            if (StringUtils.isNotBlank(profile)) {
                path = "application-module1-service-" + profile + ".yml";
            }
        }catch (NullPointerException e){
            e.printStackTrace();
            System.out.println("SpringContextUtil...未加载...");
        }
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource(path));//File引入
        configurer.setProperties(yaml.getObject());
        return configurer;
    }
}
